package fr.istic.mmm.coursepoursuite;

import fr.istic.mmm.coursepoursuite.app.App;
import fr.istic.mmm.coursepoursuite.app.Options;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class OptionsActivity extends Activity {
	
	Options options;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_options);
		
		final App application = (App)getApplicationContext();
		options = application.getOptions();
		chargerDonnees();
		
		// validation du formulaire
		Button valider = (Button) findViewById(R.id.btnValiderOption);
		valider.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				sauvegarderDonnees();
				options.save();
				finish();
			}
		});
	}
	
	public void chargerDonnees(){
		final EditText editTextName = (EditText)findViewById(R.id.editTextName);
		final EditText editTextPoids = (EditText)findViewById(R.id.editTextPoids);

		// affichage des données dans les EditText
		editTextName.setText(String.valueOf(options.getNom()));
		editTextPoids.setText(String.valueOf(options.getPoids()));
	}
	
	public void sauvegarderDonnees(){
		final EditText editTextName = (EditText)findViewById(R.id.editTextName);
		final EditText editTextPoids = (EditText)findViewById(R.id.editTextPoids);

		// récupération des données
		options.setPoids(Double.parseDouble(editTextPoids.getText().toString()));
		options.setNom(editTextName.getText().toString());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.action_accueil:
			Intent accueilIntent = new Intent(getApplicationContext(), MainActivity.class);
			startActivity(accueilIntent);
			return true;
		case R.id.action_options:
			return true;
		case R.id.action_historique:
			Intent historiqueIntent = new Intent(getApplicationContext(), HistoriqueActivity.class);
			startActivity(historiqueIntent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}
